package com.hex.ds.hdtp.core.web.data.transfer.controller;

import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.transfer.dto.request.DataTransferListRequestDto;
import com.hex.ds.hdtp.core.app.data.transfer.service.IDataTransferListService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.data.transfer.converter.IDataTransferListVoConverter;
import com.hex.ds.hdtp.core.web.data.transfer.vo.query.DataTransferListQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.web.data.transfer.controller
 * @ClassName DataTransferListController
 * @Description: 数据迁移清单-WEB接口
 * @Author: wz.li
 * @Date 2023/8/17 20:31
 * @Version v2.0.2
 **/
@Slf4j
@RestController
@Api(tags = "02-数据迁移：清单")
@RequestMapping("/api/data/transfer/list")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DataTransferListController {

    private final IDataTransferListService dataTransferListService;

    private final IDataTransferListVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [dataTransferListQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/17 20:35 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("数据迁移数据清单-分页查询")
    public ResponseEntity queryListByPage(DataTransferListQueryVo dataTransferListQueryVo, PageVo pageVo) {
        log.debug("进入到【DataTransferListController.queryListByPage】方法,参数：{}" + dataTransferListQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        DataTransferListRequestDto requestDto = converter.toDtoForQueryVo(dataTransferListQueryVo);
        return ResponseEntity.ok(dataTransferListService.queryListByPage(requestDto, pageDto));
    }

    /**
     * @Method exportDataTransferTask
     * @Param pkIds
     * @Return org.springframework.http.ResponseEntity
     * @Description 数据迁移清单-导出清单
     * @Author Yin.Yang
     * @Date 2023/12/21 18:06
     * @Version V1.0
     */
    @Log("数据迁移清单-导出清单")
    @ApiOperation("数据迁移清单-导出清单")
    @GetMapping(value = "/exportDataTransfer")
    public ResponseEntity exportDataTransferList(@RequestParam List<String> pkIds) {
        log.info("进入了【DataTransferListController.exportDataTransferList】方法,参数: {}", pkIds);
        ByteArrayResource bar = dataTransferListService.exportDataTransferList(pkIds);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=DataTransferTemplate")
                .body(bar);
    }

    /**
     * @Method importDataTransfer
     * @Param file
     * @Return org.springframework.http.ResponseEntity
     * @Description 数据迁移清单-导入清单
     * @Author Yin.Yang
     * @Date 2023/12/21 18:06
     * @Version V1.0
     */
    @Log("数据迁移清单-导入清单")
    @PostMapping(value = {"/importDataTransfer"})
    @ApiOperation("数据迁移清单-导入清单")
    public ResponseEntity importDataTransfer(@RequestBody MultipartFile file) {
        log.info("进入【DataTransferListController.importDataTransfer】方法,参数：{}" + file.toString());
        return ResponseEntity.ok(dataTransferListService.importDataTransferList(file));
    }


}
